require 'mini_magick'

class Picture < ActiveRecord::Base
	validates_presence_of :title
	after_save :write_file
	
	belongs_to :user
	has_many :comments, :order => ":created_at", :dependent => :destroy
	has_many :votes, :dependent => :destroy
	attr_reader :quantity, :pic
	
	def image=(file_data)
		@file_data = file_data
	end

	def update_score(score)
		oldscore = self.score != nil ? self.score : 0.0
		votes = self.votes != nil ? self.votes : 0
		self.score = (oldscore * votes + score.to_i) / (votes + 1)
		self.votes = votes + 1
		self.save
	end
	
	def write_file
		# TODO - czas wykonania, przerobic fnumber i focal_length na jakąś jadalną postać, zachowanie aspect ratio dla najniejszych miniaturek
		
		if not File.exists?("#{RAILS_ROOT}/public/images/storage/#{self.id}")
			Dir.mkdir("#{RAILS_ROOT}/public/images/storage/#{self.id}")
		else
			return	# tak, wiem, że to brzydkie :P
		end
		File.open("#{RAILS_ROOT}/public/images/storage/#{self.id}/temp", "w") { |file| file.write(@file_data.read) }
		image = MiniMagick::Image.from_file("#{RAILS_ROOT}/public/images/storage/#{self.id}/temp")
		#image = MiniMagick::Image.from_blob(@file_data)
		w, h = image['%w'].to_i, image['%h'].to_i
		image.compress "JPEG"
		if w > h
			if w > 980
				image.resize "980"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/large.jpg")
				image.resize "400"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/medium.jpg")
				image.resize "80x80"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/small.jpg")
			elsif w > 400
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/large.jpg")
				image.resize "400"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/medium.jpg")
				image.resize "80x80"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/small.jpg")
			else
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/large.jpg")
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/medium.jpg")
				image.resize "80x80"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/small.jpg")
			end
		else
			if h > 980
				image.resize "x980"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/large.jpg")
				image.resize "x400"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/medium.jpg")
				image.resize "80x80"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/small.jpg")
			elsif h > 400
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/large.jpg")
				image.resize "x400"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/medium.jpg")
				image.resize "80x80"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/small.jpg")
			else
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/large.jpg")
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/medium.jpg")
				image.resize "80x80"
				image.write("#{RAILS_ROOT}/public/images/storage/#{self.id}/small.jpg")
			end
		end
		self.camera = image['%[EXIF:model]']
		self.exposure_time = image['%[EXIF:exposuretime]']
		self.fnumber = image['%[EXIF:fnumber]']
		self.iso_speed = image['%[EXIF:isospeedratings]']
		self.focal_length = image['%[EXIF:focallength]']
		File.delete("#{RAILS_ROOT}/public/images/storage/#{self.id}/temp")
		self.save
	end
	
end
